package apiWeb

import (
	terminalResource "ZHONGYIHANGYAN/business/terminalResource"
	"fmt"

	"ZHONGYIHANGYAN/web"

	requset "ZHONGYIHANGYAN/bean/request"

	"github.com/gin-gonic/gin"
)

func (api *Api) TerminalResourceRouters(g *gin.RouterGroup) {
	routerG := g.Group("/terminalResource")
	{
		// 获取终端资源列表
		routerG.POST("/getTerminalResourceList", web.NewWebRouter(api.getTerminalResourceList).Func)
		// 添加或修改终端资源
		routerG.POST("/addOrUpdateTerminalResource", web.NewWebRouter(api.addOrUpdateTerminalResource).Func)
		// 删除终端资源
		routerG.POST("/deleteTerminalResource", web.NewWebRouter(api.deleteTerminalResource).Func)
		// 获取终端资源详情
		routerG.POST("/getTerminalResourceDetail", web.NewWebRouter(api.getTerminalResourceDetail).Func)
	}
}

func (api *Api) InitTerminalResourceRouter() error {
	api.terminalResourceManage = terminalResource.Manage()
	api.web.RegRouters(api.TerminalResourceRouters)
	return nil
}

// 获取终端资源列表
func (api *Api) getTerminalResourceList(c *gin.Context, replayData *interface{}) error {
	query := requset.GetTerminalResourceListQuery{}
	if err := api.web.GetPayload(c, &query); err != nil {
		return err
	}
	infoList, err := api.terminalResourceManage.GetTerminalResourceList(&query)
	if err != nil {
		return err
	}
	*replayData = infoList
	return nil
}

// 添加或修改终端资源
func (api *Api) addOrUpdateTerminalResource(c *gin.Context, replayData *interface{}) error {
	info := requset.AddOrUpdateTerminalResourceQuery{}
	if err := api.web.GetPayload(c, &info); err != nil {
		return err
	}
	if err := api.terminalResourceManage.AddOrUpdateTerminalResource(&info); err != nil {
		return err
	}
	return nil
}

// 删除终端资源
func (api *Api) deleteTerminalResource(c *gin.Context, replayData *interface{}) error {
	query := requset.DeleteTerminalResourceQuery{}
	if err := api.web.GetPayload(c, &query); err != nil {
		return err
	}
	if len(query.TerminalResourceIds) == 0 {
		return fmt.Errorf("请传入需要删除的终端资源id")
	}
	if err := api.terminalResourceManage.DeleteTerminalResource(query.TerminalResourceIds); err != nil {
		return err
	}
	return nil
}


func (api *Api) getTerminalResourceDetail(c *gin.Context, replayData *interface{}) error {
	query := requset.GetTerminalResourceDetailQuery{}
	if err := api.web.GetPayload(c, &query); err != nil {
		return err
	}
	info, err := api.terminalResourceManage.GetTerminalResourceDetail(&query)
	if err != nil {
		return err
	}
	*replayData = info
	return nil
}